// 立即执行函数 IIFE 
// 导入js文件 函数内部代码 自动执行 (function(){})(xx,xx)
(function flexible(window, document) {
  var docEl = document.documentElement
  var dpr = window.devicePixelRatio || 1

  // adjust body font size
  function setBodyFontSize() {
    if (document.body) {
      // document.body.style.fontSize = (10 * dpr) + 'px'
    }
    else {
      document.addEventListener('DOMContentLoaded', setBodyFontSize)
    }
  }
  setBodyFontSize();

  // set 1rem = viewWidth / 10
  // 1rem  = 32px
  // 1rem  = 37.5px
  //1rem =  414px/10 = 41.4px
  // 1rem = 414px/41.4= 10px

  function setRemUnit() {
    // rem em 
    // cm  mm pt dpr px em rem 
    // em： 根字体大小 16px   1em=16px   1rem=16px
    // em    rem 
  //  1rem = 10px
  // 41.4 :如果设计图 414px  1rem =10px
  //375 ===37.5   1rem=10px
  // 1080 ===108  1rem=10px
    var rem = docEl.clientWidth / 41.4
    docEl.style.fontSize = rem + 'px'
  }

  setRemUnit()

  // reset rem unit on page resize
  window.addEventListener('resize', setRemUnit)
  window.addEventListener('pageshow', function (e) {
    if (e.persisted) {
      setRemUnit()
    }
  })

  // detect 0.5px supports
  if (dpr >= 2) {
    var fakeBody = document.createElement('body')
    var testElement = document.createElement('div')
    testElement.style.border = '.5px solid transparent'
    fakeBody.appendChild(testElement)
    docEl.appendChild(fakeBody)
    if (testElement.offsetHeight === 1) {
      docEl.classList.add('hairlines')
    }
    docEl.removeChild(fakeBody)
  }
}(window, document))
